﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SurroundCat
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Width = root.Width;
            CreateStage();
        }


        int paging = 5;
        int coll = 11;
        double diameter = 0;
        double Width;

        List<RicInfo> points = new List<RicInfo>();

        /// <summary>
        /// 创建舞台（计算舞台H值）
        /// </summary>
        void CreateStage()
        {
            diameter = Width / (coll + 2);
            var radius = diameter / 2;

            for (int i = 0; i < coll; i++)
            {
                bool IsDou = i % 2 == 1;
                var top = i * diameter + paging;
                for (int j = 0; j < coll; j++)
                {
                    var lift = j * diameter + paging;
                    if (IsDou)
                        lift += radius;
                    var ell = new RicInfo(diameter, new Point(lift, top));
                    ell.SetIntercept += SetIntercept;
                    ell.Coordinate = new Point(i + 1, j + 1);
                    root.Children.Add(ell.Control);
                    root.Children.Add(ell.SetC(coll));
                    points.Add(ell);
                }
            }
        }

        RicInfo Cat;
        /// <summary>
        /// 创建猫
        /// </summary>
        void CreateCat()
        {
            if (Cat != null)
                root.Children.Remove(Cat.Control);
            int one = coll * coll % 2 > 1 ? 1 : 0;
            var ric = points[coll * coll / 2 + one];
            Cat = new RicInfo(diameter - 2, ric.Site, "#000000");
            Cat.Coordinate = ric.Coordinate;
            root.Children.Add(Cat.Control);
        }
        /// <summary>
        /// 设置墙（随机8个墙）
        /// 清空舞台，重新设置
        /// </summary>
        /// <param name="count"></param>
        void SetWall(int count = 8)
        {
            if (points?.Count <= count) return;
            points.ForEach(s => { s.IsWall = false; s.IsRoute = false; });
            dist.Clear();
            CreateCat();
            Random rd = new Random();
            for (int i = 0; i < count; i++)
            {
                var p = rd.Next(0, points.Count);
                if (p == 61)
                    p = rd.Next(0, points.Count);
                var cont = points[p];
                cont.IsWall = true;
            }
        }
        private void Bnt_Go(object sender, RoutedEventArgs e)
        {
            SetWall();
        }


        List<RicInfo> dist = new List<RicInfo>();

        private void SetIntercept(RicInfo r)
        {
            var p = points.First(s => s.Equals(r));
            p.IsWall = true;
            dist.Remove(p);
            dist.ForEach(s => points[points.IndexOf(s)].IsRoute = false);
            dist.Clear();
            var d = Sum(Cat);
            if (d == null)
            {
                MessageBox.Show("你赢了！");
                return;
            }
            SetCat(d[0]);
        }

        /// <summary>
        /// 循环周边是否有路径
        /// </summary>
        /// <param name="ric">目标点</param>
        /// <returns></returns>
        List<RicInfo> Sum(RicInfo ric)
        {
            //获取周边
            var ps = GetNextBu(ric);
            if (ps?.Count <= 0) return null;//周边没了就是堵死了
            var p = ps.OrderBy(s => s.H).ToList().First();//获取直线距离最近的
            dist.Add(p);

            if (p.Coordinate.X == coll || p.Coordinate.Y == coll || p.H == 1 || p.Coordinate.X == 1 || p.Coordinate.Y == 1)
            {
                dist.ForEach(s => s.IsRoute = true);
            }
            else
            {
                //递归缺少回退点判断，导致寻路死循环。
                 Sum(p); 
            }
            return dist;
        }


        /// <summary>
        /// 移动猫
        /// </summary>
        /// <param name="r">目标位置点</param>
        void SetCat(RicInfo r)
        {
            Cat.Coordinate = r.Coordinate;
            Cat.Site = r.Site;
            if (r.H == 1)
            {
                MessageBox.Show("你输了！");
                SetWall();
            }
        }

        /// <summary>
        ///获取下一步点集合(排除墙)
        ///   1  2
        /// 3  *  4
        ///   5  6
        /// <summary>
        List<RicInfo> GetNextBu(RicInfo r)
        {
            var x = r.Coordinate.X;
            var y = r.Coordinate.Y;
            var a = points.FindAll(s =>
            {
                return
                  (!s.IsWall && s.Coordinate.X == x - 1 && s.Coordinate.Y == y)
               || (!s.IsWall && s.Coordinate.X == x - 1 && s.Coordinate.Y == y + 1)
               || (!s.IsWall && s.Coordinate.X == x && s.Coordinate.Y == y - 1)
               || (!s.IsWall && s.Coordinate.X == x && s.Coordinate.Y == y + 1)
               || (!s.IsWall && s.Coordinate.X == x + 1 && s.Coordinate.Y == y)
               || (!s.IsWall && s.Coordinate.X == x + 1 && s.Coordinate.Y == y + 1);

            });
            return a.Where(s => !dist.Contains(s)).ToList();
        }


    }
}
